# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=18.12.1
pkgrel=4
pkgdesc="An open-source, cross-platform JavaScript runtime environment (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://nodejs.org/"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
options=('!emptydirs' '!strip' '!buildflags')
source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        '0002-Fix-system-icu-build.patch'
        '0004-Define-localtime_s-for-MinGW.patch'
        '0005-Remove-.lib-suffix-on-linked-libraries.patch'
        '0006-Define-_WIN32_WINNT-in-node.gypi.patch'
        '0007-Skip-sys-resource-and-unneeded-includes.patch'
        '0008-Link-with-municode-and-extern-wmain-for-Unicode.patch'
        '0010-Fix-incorrect-test-assumptions-for-MinGW.patch'
        '0011-Properly-detect-architecture.patch'
        '0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch'
        '0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch'
        '0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch'
        '0017-Do-not-get-confused-by-the-presence-of-winpthread.patch'
        '0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch'
        '0022-Disable-MSVC_HACK.patch'
        '0023-Make-sure-that-__rdtsc-is-declared.patch'
        '0024-Fix-gyp-mingw.patch'
        '0101-node-buildflags-fixes.patch'
        '0102-set-cxx-standard-to-gnu.patch'
        '0103-fix-unqualified-std-cast-call.patch'
        '0104-v8-fix-static-assert-implementations.patch'
        '0105-v8-fix-conflicting-macros.patch'
        '0106-v8-support-clang-in-mingw-mode.patch'
        '0107-v8-disable-system-instrumentation.patch'
        '0108-v8-buildflags-fixes.patch'
        '0109-v8-builtin-deps-buildflags-fixes.patch'
        'node')

prepare() {
  cd "${srcdir}/node-v${pkgver}"

  patch -Np1 -i "${srcdir}/0002-Fix-system-icu-build.patch"
  patch -Np1 -i "${srcdir}/0004-Define-localtime_s-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0005-Remove-.lib-suffix-on-linked-libraries.patch"
  patch -Np1 -i "${srcdir}/0006-Define-_WIN32_WINNT-in-node.gypi.patch"
  patch -Np1 -i "${srcdir}/0007-Skip-sys-resource-and-unneeded-includes.patch"
  patch -Np1 -i "${srcdir}/0008-Link-with-municode-and-extern-wmain-for-Unicode.patch"
  patch -Np1 -i "${srcdir}/0010-Fix-incorrect-test-assumptions-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0011-Properly-detect-architecture.patch"
  patch -Np1 -i "${srcdir}/0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch"
  patch -Np1 -i "${srcdir}/0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch"
  patch -Np1 -i "${srcdir}/0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch"
  patch -Np1 -i "${srcdir}/0017-Do-not-get-confused-by-the-presence-of-winpthread.patch"
  patch -Np1 -i "${srcdir}/0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch"
  patch -Np1 -i "${srcdir}/0022-Disable-MSVC_HACK.patch"
  patch -Np1 -i "${srcdir}/0023-Make-sure-that-__rdtsc-is-declared.patch"
  patch -Np1 -i "${srcdir}/0024-Fix-gyp-mingw.patch"

  patch -Np1 -i "${srcdir}/0101-node-buildflags-fixes.patch"
  patch -Np1 -i "${srcdir}/0102-set-cxx-standard-to-gnu.patch"
  patch -Np1 -i "${srcdir}/0103-fix-unqualified-std-cast-call.patch"
  patch -Np1 -i "${srcdir}/0104-v8-fix-static-assert-implementations.patch"
  patch -Np1 -i "${srcdir}/0105-v8-fix-conflicting-macros.patch"
  patch -Np1 -i "${srcdir}/0106-v8-support-clang-in-mingw-mode.patch"
  patch -Np1 -i "${srcdir}/0107-v8-disable-system-instrumentation.patch"
  patch -Np1 -i "${srcdir}/0108-v8-buildflags-fixes.patch"
  patch -Np1 -i "${srcdir}/0109-v8-builtin-deps-buildflags-fixes.patch"
}

build() {
  cd "${srcdir}/node-v${pkgver}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  local opt_cpu="x64"
  if [ "${CARCH}" = "i686" ]; then
    opt_cpu="x86"
  fi

  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --dest-os=win \
    --dest-cpu=${opt_cpu} \
    --shared \
    --shared-cares \
    --shared-libuv \
    --shared-openssl \
    --shared-zlib \
    --with-intl=system-icu \
    --without-dtrace \
    --without-etw \
    "${extra_config[@]}"

  ${MINGW_PREFIX}/bin/python tools/gyp_node.py -f make
  make V=1

  # flatten the nested node_modules/ hierarchy
  test -z "$(find deps/npm/node_modules | grep '^.\{140,\}$')" ||
  (cd deps/npm &&
   cp -R . ../npm.bup &&
   rm -rf node_modules &&
   ../../out/Release/node.exe ../npm.bup/bin/npm-cli.js install --production)
}

check() {
  cd "${srcdir}/node-v${pkgver}"

  # A few tests still fail due to a bug with pausing/resuming file streams
  ${MINGW_PREFIX}/bin/python tools/test.py --mode=release -J \
    cctest doctool inspector message parallel sequential tick-processor || true
}

package() {
  cd "${srcdir}/node-v${pkgver}"

  ${MINGW_PREFIX}/bin/python tools/install.py install "${pkgdir}" "/${MINGW_PREFIX}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"
  cp -r doc/api/{*.html,assets} "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"

  mv -v "${pkgdir}$MINGW_PREFIX/bin/node"{.exe,_exe}
  install -Dm755 "${srcdir}/node" "${pkgdir}${MINGW_PREFIX}/bin/node"
  mv -v "${pkgdir}$MINGW_PREFIX/bin/node"{_exe,.exe}

  install -Dm755 "${srcdir}/node-v${pkgver}/deps/npm/bin/npm.cmd" "${pkgdir}${MINGW_PREFIX}/bin/npm.cmd"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"
}

sha256sums=('4fa406451bc52659a290e52cfdb2162a760bd549da4b8bbebe6a29f296d938df'
            '61023beecb4e1476c2bb177262a0c7f0847f50a1a09365b290df9e822ca8d881'
            'cba59c2a92d75e01f8417da130269d34b1db13995b647a00a03de47ff2ccfe1c'
            '94319eb9bdec87a5cf1ccaacabce6071f2f57be555d20ee4feac317988296730'
            '5797d5679b5515e55bc23d6d020eabffc588b3aa640350bb3b729dd5a23eb69b'
            'ba7a4117f2224569766a7aba7dde1e4601e06ad1f1fc47916805e554f4d87385'
            '1cb8d957d299532499b72559dd78a71fe9ec5b1def789d85969b315db5e6b68f'
            '3a7d3a666fb894ab17c932bc5af2b88a71688f1eaac2ceaa1fcd281000bb69cb'
            '8d7fc3eb75121eebf2410cda8866ebc3475b26664a6484b298878e1523b887fc'
            '53df57a42d17303d0c09a5781d20d8ca1fc73ef322f445c2b7ccd1462835ac7d'
            '7b77d7cdc582d912e3630b2b5cd021f19b7871b2e68893776b9f43c819469b75'
            '00ede2f31482a4252d8570d290b397800725e2bd0e07c12634027581e338541e'
            '56650e5c30e890c967f062b48cf258862d9b1ff1188f3ea1d7f3b9e6e634dd4c'
            'a58de8a433edeed08d4f93cca81d7e61f0897c9e133d43914f8a21931a30c564'
            '061eccf99a98396765fbf84d7f907ad6c72b3012bf6802425afeff242c9a3f9f'
            '0fc13940d798add4949a1bff0d7f14a8bdeb231d2cec5cd2a8df29aba6ac69e7'
            '595ff6ff3103b0998ece8b9d6d5e2c8ff7156a91c041239d46842d2694ae8e9d'
            '34963cb754d38a67a0532ebb61f25001e24005ceab09f5002be7ea2f89fea257'
            'f1f0f90b93b9ca19457ac2639e224ce6f1c5dd04b5ada16b2c1694e89e7706a8'
            '2043f12dac52ad7cc4b4dd0dd94244fa324d6822d7475466aaf79948a47982e8'
            'f945c699e82805cb0cf37898c9cc8b91f0702290700b2c8ecba1ba52a5c8eb9f'
            '5a2b8525a434d7658df62751cd493793d7e5cc5bcf9a939431be931e55508898'
            'f3ae2f1e0bf844d1131e195ad0aa266994271373795c83cca26b58a5e6d84ce7'
            '7ceb86ffe552f2e3b8868da4eda49f3f189614c55b9becbc2f31b719dd9b2f05'
            '1807c908fd57455b6a5ff3f98a69354cc55e69a9debb52a7d49c4a14aed3e358'
            '6df6d1a0a9ee09c896f6d7a9b5cbb93f9b538fbc493af1d0dc2d67ee6986f12b'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
